package cn.xbtop.blog.controller;

import cn.xbtop.blog.enums.ResultEnum;
import cn.xbtop.blog.pojo.Admin;
import cn.xbtop.blog.service.AdminService;
import cn.xbtop.blog.utils.Result;
import cn.xbtop.blog.utils.ShiroUtils;
import cn.xbtop.blog.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/2/19 20:38
 */
@RestController
@RequestMapping("admin")
@Api(description = "管理员登录",value = "AdminController")
public class AdminController {

    @Autowired
    private AdminService adminService;
    /**
     * 登录方法
     *
     * @param admin
     * @return
     */
    @ApiOperation(value = "用户登录",consumes = "管理员登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<Object> login(@RequestBody Admin admin) {
        // 判断是否为空 必须做参数校验
        if (admin == null || StringUtils.isBlank(admin.getUsername()) || StringUtils.isBlank(admin.getPassword())) {
            return new Result(ResultEnum.PARAMS_NULL.getCode(), "用户名或密码错误!");
        }
        //使用shiro 登录 获取subject
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(admin.getUsername(), admin.getPassword());
        try {
            subject.login(token);

        } catch (Exception e) {
            return new Result<>(ResultEnum.PARAMS_NULL.getCode(), "用户名或密码错误!");
        }
        //登录成功
        Serializable sessionid = subject.getSession().getId();
        Map<String, Object> map = new HashMap<>(2);
        map.put("token", sessionid);
        //登录成功 返回token
        return new Result(map);
    }

    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    @ApiOperation(value = "获取当前登录用户信息",consumes = "当前登录用户信息信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Result<Admin> getInfo() {
        Admin admin = (Admin) ShiroUtils.getLoginUser();
        admin.setPassword("");
        return new Result<>(admin);
    }

    /**
     * 获取管理员信息
     * @return
     */
    @ApiOperation(value = "获取管理员信息",consumes = "获取管理员信息")
    @RequestMapping(value = "/getAdmin",method = RequestMethod.GET)
    public Result<Object> getAdmin(){
        Admin admin = adminService.getAdmin();
        return new Result<>(admin);
    }

    /**
     * 修改信息
     * @param admin
     * @return
     */
    @RequestMapping(value = "/updateAdmin",method = RequestMethod.PUT)
    public Result<Object> updateAdmin(@RequestBody Admin admin){
        adminService.updateByPrimaryKey(admin);
        return new Result<>("修改成功");
    }

    /**
     * 修改密码
     * @return
     */
    @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
    public Result<Object> updatePassword(@RequestBody Admin admin){
        adminService.updateByPrimaryKey(admin);
        return new Result<>("修改成功！");
    }
}
